class LRUCache {
  head;
  end;
  limit;
  hashMap;
  constructor(limit) {
    this.limit = limit;
    this.hashMap = new Map();
  }
  get(key) {
    let node = this.hashMap.get(key);
    if (node == null) return null;
    this.refreshNode(node)
    return node.value
  }
  put(key, value) {
    let node = this.hashMap.get(key);
    if (node == null){
      if (this.hashMap.size >= this.limit) {
        let oldKey = this.removeNode(this.head);
        this.hashMap.delete(oldKey)
      }
      node = new MyNode(key, value);
      this.addNode(node);
      this.hashMap.set(key, node);
    } else {
      node.value = value;
      this.refreshNode(node);
    }
  }
  remove(key) {
    let node = this.hashMap.get(key);
    if (node == null) {
      return;
    }
    this.removeNode(node);
    this.hashMap.delete(key);
  }
  refreshNode(node) {
    if (node == this.end) {
      return;
    }
    this.removeNode(node);
    this.addNode(node);
  }
  removeNode(node) {
    if (node == this.head && node == this.end) {
      this.head = null;
      this.end = null;
    } else if (node == this.end) {
      this.end = this.end.pre;
      this.end.next = null;
    } else if (node == this.head) {
      this.head = this.head.next;
      this.head.pre = null;
    } else {
      node.pre.next = node.next;
      node.next.pre = node.pre;
    }
    return node.key;
  }
  addNode(node) {
    if(this.end != null) {
        this.end.next = node;
        node.pre = this.end;
        node.next = null;
    }
    this.end = node;
    if(this.head == null){
        this.head = node;
    }
  }
}

class MyNode {
  pre;
  next;
  key;
  value;
  constructor(key, value) {
    this.key = key;
    this.value = value;
  }
}

let lruCache = new LRUCache(5);

lruCache.put("001", "用户1信息");
        lruCache.put("002", "用户2信息");
        lruCache.put("003", "用户3信息");
        lruCache.put("004", "用户4信息");
        lruCache.put("005", "用户5信息");
        console.log(lruCache.get("002"));
        lruCache.put("004", "用户4信息更新");
        lruCache.put("006", "用户6信息");
        lruCache.remove('003')
        console.log(lruCache.get("001"));
        console.log(lruCache.get("006"));
        console.log(lruCache.get("003"));